--[[
 -- *名称:ThreeTypeAnalyzer
 -- *描述:三张分析器
 -- *版权:Copyright © 2016-2018 深圳市三只小熊科技有限公司 版权所有
 -- *作者:Administrator
 -- *创建日期:2017-11-09
 -- *修改日期:2017-11-09
 ]]
local DdzSuitAnalyzer = require(basePath.."suit/ddzsuitanalyzer");
local ThreeTypeAnalyzer = pokercore.class(DdzSuitAnalyzer,false);

ThreeTypeAnalyzer.TUPLE_SIZE = 3 ;
local TUPLE_SIZE = 3 ;

---
 -- [ctor 默认类的构造方法]
 -- @DateTime 2017-09-20
 -- @param     [description]
 -- @return    [description]
 --
function ThreeTypeAnalyzer:ctor(...)
    ThreeTypeAnalyzer.super.ctor(self,...) ;
end

---
 -- [dtor 默认类的销毁方法]
 -- @DateTime 2017-09-20
 -- @param     [description]
 -- @return    [description]
 --
function ThreeTypeAnalyzer:dtor(...)
	ThreeTypeAnalyzer.super.dtor(self,...) ;
end


--@Override
function ThreeTypeAnalyzer:getSuitType()
   return DdzCardTypes.THREE ;
end

--@Override
function ThreeTypeAnalyzer:doCheckRule(cards,term,result)
	if result:getCardNum() == ThreeTypeAnalyzer.TUPLE_SIZE then
		--没有癞子，都是癞子
		if (result:getLaziCardNum() == 0) or (result:isAllCardLazi()) then
			--print("ThreeTypeAnalyzer:doCheckRule 1");
			return #(result:getAllValueCounter():getValuesInCount(3)) == 1 ;
		else
			-- 两张癞子，非赖中不能有王
			if (result:getNoneLaziValueCounter():hasJoker()) then -- 非赖中含有大小王
				return false ;
			else
				return true ;
			end
		end
	else
		--print("ThreeTypeAnalyzer:doCheckRule 2");
		return false ;
	end
end

--@Override
function ThreeTypeAnalyzer:doAnalysizeLaizi(cards,term,onlyMaxValue)
	local cardsResult = {} ;
	local result = cards:getResult();
	local laiziCardList = cards:subList(result:getCardNum() - result:getLaziCardNum()+1, result:getCardNum()) ;
	local value = result:getNoneLaziValueCounter():getAllValues()[1] ;
	for _,card in pairs(laiziCardList) do
		card:changeCard(value);
	end
	local cardsCopy = cards:clone();
	self:analysize(cardsCopy,term);
	table.insert(cardsResult,cardsCopy) ;
	return cardsResult ;
end


--提牌算法

--@Override
function ThreeTypeAnalyzer:containsPossibleCards(handCards, term) 
	local result =  handCards:getResult();
	return result:getCardNum() - result:getJokerCardNum() >= TUPLE_SIZE;
end

--/** 最多使用癞子的数量 */
local MAX_USE_LAIZI_CARD_NUM = 3;


--@Override
function ThreeTypeAnalyzer:doListPossibleCards(handCards,term) 
	local cardsResult = {} ;
	--// 非癞子部分
	local result = handCards:getResult();
	local noneLaiziCardList = handCards:subList(1, result:getNoneCardNum());
	local noneLaiziCards = DdzTypeUtil.createCardsCopy(noneLaiziCardList);
	noneLaiziCards:setResult(handCards:getResult():clone());
	--// 癞子部分
 	local laiziCardList = handCards:subList(result:getNoneCardNum()+1, result:getCardNum());
	local laiziCards = DdzTypeUtil.createCardsCopy(laiziCardList);
	laiziCards:setResult(handCards:getResult():clone());

	local laiziCardNum = result:getLaziCardNum();
	local maxUseLaiziCardNum = MAX_USE_LAIZI_CARD_NUM < laiziCardNum and MAX_USE_LAIZI_CARD_NUM or laiziCardNum;
	for useLaiziCardNum = 0,maxUseLaiziCardNum do
		local needCardNum = TUPLE_SIZE - useLaiziCardNum;
		if (needCardNum > 0) then -- // 部分用癞子的情况
			local useLaiziCardList = DdzTypeUtil.cloneSublistedCards(laiziCards, 1, useLaiziCardNum);--// 补癞子
			local cardsList = {};
			if (needCardNum == 1) then
				table.addAll(cardsList,DdzTypeUtil.selectSingleCards(noneLaiziCards, false));
			else 
				table.addAll(cardsList,DdzTypeUtil.selectTupleCountCards(noneLaiziCards, needCardNum));
			end
			for _,usecards in ipairs(cardsList) do
				local card = usecards:get(1);
				for _,laiziCard in ipairs(useLaiziCardList) do
					laiziCard:changeCard(card:getValue());
				end
				local cardsCopy = DdzTypeUtil.cloneCardsAndAddCardList(usecards, useLaiziCardList);
				if (self:match(cardsCopy, term)) then
					self:analysize(cardsCopy, term);
					----dump(cardsCopy,"ThreeTypeAnalyzer cardsCopy");
					table.insert(cardsResult,cardsCopy:clone());
				end
			end
		 else --// 全部用癞子的情况
			local useLaiziCardList = DdzTypeUtil.selectTupleCountCards(laiziCards, TUPLE_SIZE);
			for _,useLaiziCards in ipairs(useLaiziCardList) do
				for _,card in ipairs(useLaiziCards) do
					card:changeCard(card:getNumber());
				end
				local cardsCopy = DdzTypeUtil.cloneCardsAndAddCardList(useLaiziCards, nil);
				if (self:match(cardsCopy, term)) then
					self:analysize(cardsCopy, term);
					table.insert(cardsResult,cardsCopy:clone());
				end
			end
		end
	end
	return cardsResult;
end
--[[
--@Override
function ThreeTypeAnalyzer:doListPossibleCards(handCards,term,onlyMaxValue)
	local cardsResult = {} ;
	local result = handCards:getResult();
	local noneLaiziValues = result:getNoneLaziValueCounter():getAllValues();
	--直接寻找三张
	for _,valueTemp in ipairs(noneLaiziValues) do
		if result:getNoneLaziValueCounter():getValueCount(valueTemp) == ThreeTypeAnalyzer.TUPLE_SIZE then
			local partCardList = DdzTypeUtil.obtainsPartCards(handCards, valueTemp, ThreeTypeAnalyzer.TUPLE_SIZE);
			table.insert(cardsResult,DdzTypeUtil.createCloneCards(partCardList));
			DdzTypeUtil.revert2Cards(handCards, partCardList);
		end
	end
	--拆牌(三张,炸弹) 获取三张
	for _,valueTemp in ipairs(noneLaiziValues) do
		if result:getNoneLaziValueCounter():getValueCount(valueTemp) > ThreeTypeAnalyzer.TUPLE_SIZE then
			local partCardList = DdzTypeUtil.obtainsPartCards(handCards, valueTemp, ThreeTypeAnalyzer.TUPLE_SIZE);
			table.insert(cardsResult,DdzTypeUtil.createCloneCards(partCardList));
			DdzTypeUtil.revert2Cards(handCards, partCardList);
		end
	end

	local laiziCardNum = result:getLaziCardNum();
	if laiziCardNum > 0 then
		local laiziValues = result:getLaziValueCounter():getAllValues();
		--补一张癞子
		local pairCards = DdzTypeUtil.selectPairCards(handCards);
		for _,valueTemp in ipairs(laiziValues) do
			if (result:getLaziValueCounter():getValueCount(valueTemp) == 1) then -- // 落单的癞子
				local partCardList = DdzTypeUtil.obtainsPartCards(handCards, valueTemp, 1);
				for _,cardsTemp in ipairs (pairCards) do
					partCardList[1]:changeCard(cardsTemp:get(1));
					table.insert(cardsResult,DdzTypeUtil.createCloneCards2(cardsTemp, partCardList));
				end
				DdzTypeUtil.revert2Cards(handCards, partCardList);
			end
		end
		for _,valueTemp in ipairs(laiziValues) do
			if (result:getLaziValueCounter():getValueCount(valueTemp) > 1) then -- // 多张的癞子
				local partCardList = DdzTypeUtil.obtainsPartCards(handCards, valueTemp, 1);
				for _,cardsTemp in ipairs(pairCards) do
					partCardList[1]:changeCard(cardsTemp:get(1));
					table.insert(cardsResult,DdzTypeUtil.createCloneCards2(cardsTemp, partCardList));
				end
				DdzTypeUtil.revert2Cards(handCards, partCardList);
			end
		end
		if (laiziCardNum > 1) then
			-- 补两张癞子
			for _,valueTemp in ipairs(laiziValues) do
				if (result:getLaziValueCounter():getValueCount(valueTemp) > 1) then --// 多张的癞子
					local partCardList = DdzTypeUtil.obtainsPartCards(handCards, valueTemp, 2);
					for _,card in ipairs(partCardList) do
						card:changeCard(card:getNumber());
					end
					table.insert(cardsResult,DdzTypeUtil.createCloneCards(partCardList));
					DdzTypeUtil.revert2Cards(handCards, partCardList);
				end
			end
			if (laiziCardNum > 2) then
				--// 补三张癞子
				for _, valueTemp in ipairs(laiziValues) do
					if (result:getLaziValueCounter():getValueCount(valueTemp) > 3) then -- // 多张的癞子
						local partCardList = DdzTypeUtil.obtainsPartCards(handCards, valueTemp, 3);
						for _,card in ipairs(partCardList) do
							card:changeCard(card:getNumber());
						end
						table.insert(cardsResult,DdzTypeUtil.createCloneCards(partCardList));
						DdzTypeUtil.revert2Cards(handCards, partCardList);
					end
				end
			end
		end
	end
	return cardsResult;
end
]]
return ThreeTypeAnalyzer;

